package com.cloud.rou.im.connection.userstatus;


import com.cloud.rou.im.connection.container.manager.SessionManager;
import com.cloud.rou.im.connection.container.manager.SessionManagerHolder;
import com.cloud.rou.im.connection.container.session.ConnectSessionFactory;
import com.cloud.rou.im.connection.container.session.Session;
import io.netty.channel.ChannelHandlerContext;

/**
 * <p></p>
 *
 * @author GaoShuangWen
 */
public class UserStatusServiceImpl implements UserStatusService {

    /**
     * 用户上线，存储userId与机器id的关系
     *
     * @param userId
     * @return 如果当前用户在线，则返回他连接的机器id，否则返回null
     */
    @Override
    public Long online(Long userId, ChannelHandlerContext ctx) {
        Session session = SessionManagerHolder.getManager().createSession(userId, ctx);
        return session.getUserId();
    }

    /**
     * 用户下线
     *
     * @param userId
     */
    @Override
    public void offline(Long userId) {
        SessionManager manager = SessionManagerHolder.getManager();
        manager.removeConnectSession(userId);
    }

}
